<?php
/**
 * Classe déléguée à la gestion des champs d'un formulaire
 *
 * @category librairies
 * @package sh
 *
 * @author Mickaël CASTANHEIRO
 * @version 1.0
 */
class shField extends shBase {
	/*
    * Constantes
	*/
	const DATE = 0;
	const SELECT = 1;
	const TEXT = 2;
	const EMAIL = 3;
	const TEXTAREA = 4;
	const CHECKBOX = 5;
	const PASSWORD = 6;
	const MULTIBOX = 7;
	const HOUR = 8;
	const STRING = 9;
	const COLOR = 10;


	const LINK = 20;
	const BUTTON = 21;
	const RESET = 22;


	/**
	 * @access protected
	 * @var shFormulaire
	 */
	protected $formulaire;

	/**#@+
     * @access protected
     * @var string
	 */
	protected $name;
	protected $label;
	protected $type;
	protected $value;
	protected $printableType;
	protected $helper;
	protected $error;
	protected $required;
	/**#@-*/

	/**
	 * Options des boîtes de sélection ou radio
	 * @access protected
	 * @var array
	 */
	protected $options;

	/**
	 * Constructeur de la classe
	 */
	public function __construct() {
		$this->required = true;
	}

	/**
	 * Détermine la valeur
	 * @param <mixed> valeur
	 * @return <shField> this
	 */
	public function setValue($_value) {
		if ( $this->getType() == shField::CHECKBOX ) {
			if ( !$_value ) $this->value = false;
			else $this->value = true;
		}
		else {
			parent::setValue($_value);
		}
		return $this;
	}

	/**
	 * Lie le champ à un formulaire
	 * @param <shFormulaire> Formulaire ciblé
	 */
	public function appendTo(shFormulaire $_formulaire) {

		if ( ($this->type == shField::LINK) OR ($this->type == shField::BUTTON ) OR ($this->type == shField::RESET ) ) {
			$_formulaire->appendButton($this);
		}
		else {
			$_formulaire->append($this);
		}

		$this->formulaire = &$_formulaire;
		return $this;
	}

	/**
	 * Soumet le champ lorsque le formulaire est soumis
	 */
	public function execute() {
		// Pour les checkbox
		if ( $this->type == shField::CHECKBOX ) {
			$this->value =  isset($_POST[$this->name]);
		}


		// Pour les multibox
		elseif ( $this->type == shField::MULTIBOX ) {
			if ( !empty($_POST[$this->name]) )
				$this->value =  $_POST[$this->name];
			else
				$this->value = array();
		}


		// Pour les autres
		else {
			$this->value = (!empty($_POST[$this->name])) ? trim($_POST[$this->name]) : null;
		}
		$this->check();
	}

	/**
	 * Ajoute un helper au helper existant
	 * @param <strong> Texte de l'aide
	 */
	public function addHelper($_help) {
		$this->helper .= '. ' . $_help;
	}

	/**
	 * Vérifie l'intégrité du champ
	 */
	public function check() {
		// Champ requis ?
		if ( $this->required ) {
			if ( ($this->type == self::MULTIBOX) AND empty($this->value) ) {
				$this->error = 'Sélectionner au moins un élément';
				return false;
			}
			elseif ( ($this->type != self::CHECKBOX) AND $this->required AND ($this->value == '') ) {
				$this->error = 'Champs requis';
				return false;
			}
		}

		switch ( $this->type ) {
			case self::DATE:
				if ( !empty($this->value) AND !preg_match('!^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)[0-9]{2}$!', $this->value) )
					$this->error = 'Format dd/mm/yyyy';
				return false;
				break;

			case self::HOUR:
				if ( !empty($this->value) AND !preg_match('!^([012][0-9])\:([0-5][0-9])((\:([0-5][0-9]))?)$!', $this->value) )
					$this->error = 'Format hh:mm(:ss)';
				return false;
				break;

			case self::EMAIL:
				if ( !empty($this->value) AND !preg_match('#^[\w.\+-]+@[\w.\+-]+\.[a-zA-Z]{2,6}$#', $this->value) )
					$this->error = 'Format adresse@domaine.ndt';
				return false;
				break;

			default:
				break;
		}
	}

	/**
	 * Affiche le champ
	 */
	public function display() {
		$template = new shTemplate;
		$template->setPath(DIR_TEMPLATES . '_field.tpl');
		$template->addData('field', $this)
			   ->addData('formulaire', $this->formulaire);
		$template->display();
	}


	/**
	 * Crée un shField de type texte
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function text($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::TEXT);
		$field->setPrintableType(shField::TEXT);
		return $field;
	}


	/**
	 * Crée un shField de type email
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function email($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::EMAIL);
		$field->setPrintableType(shField::TEXT);
		$field->setHelper('Format adresse@domaine.ndt');
		return $field;
	}


	/**
	 * Crée un shField de type textarea
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function textarea($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::TEXTAREA);
		$field->setPrintableType(shField::TEXTAREA);
		return $field;
	}


	/**
	 * Crée un shField de type password
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function password($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::PASSWORD);
		$field->setPrintableType(shField::PASSWORD);
		return $field;
	}

	/**
	 * Crée un shField de type date
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function date($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setHelper('Format dd/mm/yyyy');
		$field->setType(shField::DATE);
		$field->setPrintableType(shField::TEXT);
		return $field;
	}

	/**
	 * Crée un shField de type hour
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function hour($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setHelper('Format hh:mm(:ss)');
		$field->setType(shField::HOUR);
		$field->setPrintableType(shField::TEXT);
		return $field;
	}

	/**
	 * Crée un shField de type select
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function select($_name) {
		$field = new shField;
		$field->setOptions(array());
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::SELECT);
		$field->setPrintableType(shField::SELECT);
		return $field;
	}

	/**
	 * Crée un shField de type checkbox
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function checkbox($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::CHECKBOX);
		$field->setPrintableType(shField::CHECKBOX);
		$field->setValue(false);
		$field->setRequired(false);
		return $field;
	}

	/**
	 * Crée un shField de type string & hidden
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function string($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::STRING);
		$field->setPrintableType(shField::STRING);
		$field->setRequired(false);
		return $field;
	}

	/**
	 * Crée un shField de type link
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function link($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::LINK);
		$field->setPrintableType(shField::LINK);
		return $field;
	}


	/**
	 * Crée un shField de type select multiple
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function multibox($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::MULTIBOX);
		$field->setPrintableType(shField::MULTIBOX);

		$field->setValue(array());
		return $field;
	}


	/**
	 * Crée un shField de type couleur
	 * @param <string> Nom du champ
	 * @return <shField> Champ crée
	 */
	public static function color($_name) {
		$field = new shField;
		$field->setName(escapeString($_name));
		$field->setLabel($_name);
		$field->setType(shField::COLOR);
		$field->setPrintableType(shField::TEXT);
		$field->setHelper('Format #fff ou #ffffff');

		return $field;
	}

}
?>